package com.iris.live.services.report.sources;

import com.iris.live.services.data.repositories.FinanceReceivablesRepository;
import com.iris.live.services.models.PageData;
import com.iris.live.services.report.Cell;
import com.iris.live.services.report.IExcelReportSource;
import com.iris.live.services.report.Row;
import com.iris.live.services.report.Sheet;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 财务-导出款项历史
 */
@Component("ExportMoneyHistorySource")
public class ExportMoneyHistorySource implements IExcelReportSource{

    @Resource
   private FinanceReceivablesRepository financeReceivablesRepository;

    @Override
    public Sheet[] getSheets(Map<String, Object> context) {
        Sheet[] sheets = new Sheet[1];
        sheets[0] = new Sheet("财务款项历史导出模板","财务款项历史导出模板",true);
        return sheets;
    }

    @Override
    public List<Row> getSheetRows(Map<String, Object> context, Sheet sheet) throws ParseException {
        // 获取参数
        Map params = (Map) context.get("param");
        String quotedNum = params.get("quotedNum") == null ? "" : params.get("quotedNum").toString();
        String pageSize = params.get("pageSize") == null ? "0" : params.get("pageSize").toString();
        String pageIndex = params.get("pageIndex") == null ? "0" : params.get("pageIndex").toString();

        List<Row> rows = new ArrayList<>();
        // 空行输出
        int index;
        for(index = 0; index < 6; index++) {
            rows.add(new Row());
        }

        // 获取数据
        PageData pageData = financeReceivablesRepository.getMoneyHistory(quotedNum,Integer.valueOf(pageSize),Integer.valueOf(pageIndex));
        int rowIndex = 1; // 行标
        if (null != pageData) {
            List datas = pageData.getData();
            if (null != datas) {
                for (Object o : datas) {
                    rows.add(new Row(getCells(rowIndex++,o)));
                }
            }
        }
        return rows;
    }

    private Cell[] getCells (int rowIndex,Object o) {
        Map map = (Map) o;
        Cell[] cells = new Cell[12];
        // 序号列
        cells[0] = new Cell(rowIndex + "",0);
        // 数据列
        int colIndex = 1;  // 列标
        cells[colIndex] = new Cell(map.get("账目日期") == null ? "" : map.get("账目日期").toString(), colIndex++);
        cells[colIndex] = new Cell(map.get("账目单号") == null ? "" : map.get("账目单号").toString(), colIndex++);
        cells[colIndex] = new Cell(map.get("账目类型") == null ? "" : map.get("账目类型").toString(), colIndex++);
        cells[colIndex] = new Cell(map.get("支付方式/挂账类型") == null ? "" : map.get("支付方式/挂账类型").toString(), colIndex++);
        cells[colIndex] = new Cell(map.get("账目金额") == null ? "" : map.get("账目金额").toString(), colIndex++);
        cells[colIndex] = new Cell(map.get("交易凭证号") == null ? "" : map.get("交易凭证号").toString(), colIndex++);
        cells[colIndex] = new Cell(map.get("备注") == null ? "" : map.get("备注").toString(), colIndex++);
        cells[colIndex] = new Cell(map.get("是否红冲") == null ? "" : map.get("是否红冲").toString(), colIndex++);
        cells[colIndex] = new Cell(map.get("操作人") == null ? "" : map.get("操作人").toString(), colIndex++);
        cells[colIndex] = new Cell(map.get("传送结果") == null ? "" : map.get("传送结果").toString(), colIndex++);
        cells[colIndex] = new Cell(map.get("传送日期") == null ? "" : map.get("传送日期").toString(), colIndex++);
        return cells;
    }

}
